Migrate region discovery to IMDS /compute JSON endpoint#929
Conversation
There was a problem hiding this comment.
Pull request overview
This PR migrates Azure VM region auto-detection in MSAL Python from the legacy IMDS text endpoint (/compute/location?format=text) to the newer IMDS JSON compute endpoint (/compute), aligning with the newer metadata API behavior while preserving existing region validation and fallback behavior.
Changes:
- Updated
_detect_region_of_azure_vmto call the IMDS/compute?api-version=2021-02-01endpoint and extractlocationfrom the JSON payload. - Added unit tests covering valid/missing/null/malformed
location, invalid region values, network failure, and the updated request URL/headers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| msal/region.py | Switches IMDS endpoint to /compute JSON response and parses location for region validation. |
| tests/test_region.py | Adds focused tests to validate the new IMDS URL/headers and JSON parsing fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
/azp run MSAL.Python-PR-OneBranch-Official |
1 similar comment
|
/azp run MSAL.Python-PR-OneBranch-Official |
|
@copilot rebase |
|
@RyAuld Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub. You can ask me to try again later by mentioning me in a new comment. If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: Sorry for the inconvenience! |
Switch Azure VM region auto-detection from the legacy IMDS text endpoint (/metadata/instance/compute/location?format=text) to the JSON endpoint (/metadata/instance/compute?api-version=2021-02-01), reading the region from the 'location' field. Missing/null location and malformed JSON fall back to None as before. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Treat a non-string resp.text (json.loads raising TypeError) and a non-string location field as malformed IMDS responses and fall back to None, so region auto-detection cannot raise during client setup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the IMDS compute metadata api-version into a module-level _IMDS_API_VERSION constant so future version bumps are a single, self-documenting change, consistent with msal-go and msal-java. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
41346a3 to
cb0c5ef
Compare
The rebase completed locally (3 commits cleanly on top of git fetch origin
git checkout rginsburg/imds_compute_json
git rebase origin/dev
git push --force-with-lease |
Ports microsoft-authentication-library-for-dotnet#6057 to MSAL Python.
What
Migrates Azure VM region auto-detection from the legacy IMDS text endpoint to the newer JSON endpoint.
Details
Tests
Added
TestDetectRegionOfAzureVmto ests/test_region.py: valid location, missing/null location, malformed JSON, invalid region value, network failure, and an assertion on the new request URL/headers. All 20 tests in ests/test_region.py pass.